package com.lagou.edu.service.impl;

import com.lagou.edu.dao.LagouTokenDao;
import com.lagou.edu.dao.LagouUserDao;
import com.lagou.edu.enums.CodeStatus;
import com.lagou.edu.pojo.LagouToken;
import com.lagou.edu.pojo.LagouUser;
import com.lagou.edu.service.CodeFeignClient;
import com.lagou.edu.service.UserService;
import com.lagou.edu.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.UUID;

/**
 * @author zjy
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    LagouTokenDao lagouTokenDao;

    @Autowired
    LagouUserDao lagouUserDao;

    @Autowired
    CodeFeignClient codeFeignClient;

    private static String SESSION_ID = "lagouSessionId";

    @Override
    public Result register(String email, String password, String code, HttpServletResponse response) {
        if(isRegistered(email)){
            return Result.error("用户已注册，请直接登录");
        }
        Integer validateCode = codeFeignClient.validateCode(email, code);
        //验证码正确
        if(Objects.equals(validateCode, CodeStatus.RIGHT.getCode())){
            //存储用户信息
            LagouUser user = new LagouUser();
            user.setEmail(email);
            user.setPassword(password);
            lagouUserDao.save(user);
            //存储token信息，因为注册成功后会直接调整到欢迎页，需要生产一下token
            String token = UUID.randomUUID().toString();
            LagouToken lagouToken = new LagouToken();
            lagouToken.setEmail(email);
            lagouToken.setToken(token);
            lagouTokenDao.save(lagouToken);
            Cookie cookie = new Cookie(SESSION_ID,token);
            cookie.setPath("/");
            response.addCookie(cookie);
            return Result.ok();
        }else{
            String msg = CodeStatus.getDesc(validateCode);
            return Result.error(msg);
        }
    }

    @Override
    public Boolean isRegistered(String email) {
        LagouUser user = lagouUserDao.findByEmail(email);
        if(user == null){
            return false;
        }
        return true;
    }

    @Override
    public Result login(String email, String password, HttpServletResponse response) {
        System.out.println("Resultemail:" + email + "password:" + password);

        if(!isRegistered(email)){
            return Result.error("用户未注册,请先注册！");
        }
        LagouUser lagouUser = lagouUserDao.findByEmailAndPassword(email, password);
        if(lagouUser == null){
            return Result.error("用户名或密码错误");
        }
        String token = UUID.randomUUID().toString();
        LagouToken lagouToken = lagouTokenDao.findByEmail(email);
        if(lagouToken == null){
            lagouToken = new LagouToken();
        }
        //更新token
        lagouToken.setToken(token);
        lagouTokenDao.save(lagouToken);
        Cookie cookie = new Cookie(SESSION_ID,token);

        //不设置这个浏览器获取不到cookie
        cookie.setPath("/");

        response.addCookie(cookie);



        return Result.ok(email);
    }

    @Override
    public Result userInfo(String token) {
        LagouToken lagouToken = lagouTokenDao.findByToken(token);
        if(lagouToken != null){
            return Result.ok(lagouToken.getEmail());
        }
        return Result.error("token无效，未查询到用户信息");
    }
}
